package com.axend.aerosense.dev.fragment;

import android.os.Bundle;
import android.text.Html;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.IHostLayout;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.dev.BR;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevFragmentBedCalibrationBinding;
import com.axend.aerosense.dev.entity.DevHeartArgsBean;
import com.axend.aerosense.dev.viewmodel.BedCalibrationViewModel;
import com.axend.aerosense.common.util.FragmentUtil;


import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;


public class DevBedCalibrationFragment extends MvvmBaseFragment<DevFragmentBedCalibrationBinding, BedCalibrationViewModel>
 implements ILoadDataResult<DevHeartArgsBean> {
    public final static String RADAR_CALIBRATE_RESULT = "radarCalibrateResult";
    private String radarId = "000";//000000000002

    public static DevBedCalibrationFragment getInstance() {
        return new DevBedCalibrationFragment();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        showLoading();
        initView();
    }

    private void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            radarId = bundle.getString(DevDeviceSettingsFragment.RADAR_ID, "");
        }
        viewModel.setRadarId(radarId);

        viewModel.getDevHeight().observe(getViewLifecycleOwner(), height -> {
            //            String text = String.format(getString(R.string.dev_bed_calibration_tips), String.valueOf(height));
            //保留一位小数
            DecimalFormat df = new DecimalFormat("#.0");
            String heightStr = df.format(height);
            String text = "*Are you sure your device is installed at <font color='#3578FC'>" + heightStr + "ft</font>? Please make sure that you've entered the correct number. <br><br> *Please wake sure nobody is within the <font color='#3578FC'>6.5ft</font> region from the bed. Otherwise the calibration may not be accurate.";
            viewDataBinding.devTvTips.setText(Html.fromHtml(text));
        });

        viewDataBinding.devButCalibrate.setOnClickListener(v -> {
            viewModel.getIsStartCalibrate().postValue(true);
            viewDataBinding.devTvTips.setText(R.string.dev_bed_calibration_tips_ing);
            viewModel.radarCalibrate();
        });
        Fragment that = this;
        viewModel.getIsCalibrateSuccess().observe(getViewLifecycleOwner(), aBoolean -> {
            if (viewModel.getIsStartCalibrate().getValue() != null && viewModel.getIsStartCalibrate().getValue()) {
                Fragment fragment = DevCalibrateResultFragment.getInstance();
                Bundle bundleSend = new Bundle();
                bundleSend.putBoolean(RADAR_CALIBRATE_RESULT, aBoolean);
                fragment.setArguments(bundleSend);
                FragmentActivity activity = getActivity() != null ? getActivity() : requireActivity();
                FragmentUtil.add(activity, ((IHostLayout) activity).getContentId(), fragment, that, true);
            }
        });
        viewModel.onLoad();
    }

    @Override
    public void onResume() {
        super.onResume();
//        isShowTitleBarRightText(true);
//        viewModel.onLoad();
        if (getActivity() != null) {
            ((ITitle) getActivity()).setTitle(getString(R.string.dev_calibration));
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.dev_fragment_bed_calibration;
    }

    @Override
    public int getBindingVariable() {
        return BR.data;
    }

    @Override
    protected BedCalibrationViewModel getViewModel() {
        BaseViewModelFactroy<DevHeartArgsBean> factroy = new BaseViewModelFactroy<>(this,this);
        return new ViewModelProvider(this,factroy).get(BedCalibrationViewModel.class);
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(DevHeartArgsBean baseBean) {
        showContent();
    }

    @Override
    public void loadDataFail(String decs) {

    }
}
